package com.android.gufengmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.UserBean;

import java.util.List;

public class UserManagerAdapter extends RecyclerView.Adapter<UserManagerAdapter.UserManagerHolder> {

    private Context mContext;
    private List<UserBean> mUserBeanList;
    private onCheckSelectListener mListener;

    public UserManagerAdapter(Context context, List<UserBean> userBeanList) {
        mContext = context;
        mUserBeanList = userBeanList;
    }

    @NonNull
    @Override
    public UserManagerAdapter.UserManagerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new UserManagerHolder(LayoutInflater.from(mContext).inflate(R.layout.item_user_manager, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull UserManagerAdapter.UserManagerHolder holder, int position) {
        UserBean userBean = mUserBeanList.get(position);
        holder.mTvPosition.setText("id：" + (position + 1));
        holder.mTvUser.setText("用户名：" + userBean.getUserName());
        holder.mTvPass.setText("密码：" + userBean.getUserPass());
        holder.mTvSex.setText("性别：" + userBean.getSex());
        holder.mTvAge.setText("年龄：" + userBean.getAge());
        holder.mCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            mListener.OnCheckSelect(position, isChecked);
        });
    }

    @Override
    public int getItemCount() {
        return mUserBeanList.size();
    }

    public class UserManagerHolder extends RecyclerView.ViewHolder {

        private final TextView mTvPosition;
        private final TextView mTvUser;
        private final TextView mTvPass;
        private final TextView mTvSex;
        private final TextView mTvAge;
        private final CheckBox mCheckBox;

        public UserManagerHolder(@NonNull View itemView) {
            super(itemView);

            mTvPosition = itemView.findViewById(R.id.tv_position);
            mTvUser = itemView.findViewById(R.id.tv_user);
            mTvPass = itemView.findViewById(R.id.tv_pass);
            mTvSex = itemView.findViewById(R.id.tv_sex);
            mTvAge = itemView.findViewById(R.id.tv_age);
            mCheckBox = itemView.findViewById(R.id.chx_select);
        }
    }

    public interface onCheckSelectListener {
        void OnCheckSelect(int position, boolean isCheck);
    }

    public void setOnCheckSelectListener(onCheckSelectListener onCheckSelectListener) {
        mListener = onCheckSelectListener;
    }
}
